package com.gulimall.order.config.mq;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeCloseModel;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.common.constant.OrderConstant;
import com.gulimall.order.entity.OrderEntity;
import com.gulimall.order.enume.OrderStatusEnum;
import com.gulimall.order.service.impl.OrderServiceImpl;
import com.gulimall.order.web.AlipayTemplate;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author Rookie-6688
 * @Description
 * @Create 2021-05-10 15:06
 */
@Component
@Slf4j
public class MyListener {

    @Resource
    private OrderServiceImpl orderServiceImpl;
    @Resource
    private AlipayTemplate alipayTemplate;
    /**
     * 改变订单状态
     * @param orderEntity
     * @param message
     * @param channel
     */
    @RabbitListener(queues = "order.release.order.queue")
    public void handlerOrderRelease(OrderEntity orderEntity, Message message, Channel channel) throws IOException {
        try {
            log.info("订单状态消息-------------------------------------");

            // 1、先关闭支付宝交易界面
            alipayTemplate.closeOrder(orderEntity);

            // 2、撤销订单
            orderServiceImpl.closeOrder(orderEntity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
